package day009.picture_puzzle.itheima004;

import javax.swing.*;
import java.util.Random;

public class PictureFrame extends JFrame {

    int[][] datas = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    private int x0;
    private int y0;

    public PictureFrame() {
        initFrame();
        initData();
        paintView();

        this.setVisible(true);
    }

    public void initData() {
        Random r = new Random();
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                int x = r.nextInt(datas.length);
                int y = r.nextInt(datas[i].length);

                int temp = datas[i][j];
                datas[i][j] = datas[x][y];
                datas[x][y] = temp;
            }
        }

        wc:for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                if (datas[i][j] == 0) {
                    x0 = i;
                    y0 = j;
                    break wc;
                }
            }
        }

        System.out.println(x0 + "," + y0);
    }

    public void paintView() {
        JLabel titleLabel = new JLabel(new ImageIcon("study-practice/src/day009/picture_puzzle/images/title.png"));
        titleLabel.setBounds(354, 27, 232, 57);
        this.add(titleLabel);

        JPanel imagePanel = new JPanel();
        imagePanel.setBounds(150, 114, 360, 360);
        imagePanel.setLayout(null);

        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas[i].length; j++) {
                JLabel imageLabel = new JLabel(new ImageIcon("study-practice/src/day009/picture_puzzle/images/" + datas[i][j] + ".png"));
                imageLabel.setBounds(j * 90, i * 90, 90, 90);
                imagePanel.add(imageLabel);
            }
        }
        this.add(imagePanel);

        JLabel canzhaoTuLabel = new JLabel(new ImageIcon("study-practice/src/day009/picture_puzzle/images/canzhaotu.png"));
        canzhaoTuLabel.setBounds(574, 114, 122, 121);
        this.add(canzhaoTuLabel);

        JButton shangButton = new JButton(new ImageIcon("study-practice/src/day009/picture_puzzle/images/shang.png"));
        shangButton.setBounds(732, 265, 57, 57);
        this.add(shangButton);

        JButton zuoButton = new JButton(new ImageIcon("study-practice/src/day009/picture_puzzle/images/zuo.png"));
        zuoButton.setBounds(650, 347, 57, 57);
        this.add(zuoButton);

        JButton xiaButton = new JButton(new ImageIcon("study-practice/src/day009/picture_puzzle/images/xia.png"));
        xiaButton.setBounds(732, 347, 57, 57);
        this.add(xiaButton);

        JButton youButton = new JButton(new ImageIcon("study-practice/src/day009/picture_puzzle/images/you.png"));
        youButton.setBounds(813, 347, 57, 57);
        this.add(youButton);

        JButton qiuzhuButton = new JButton(new ImageIcon("study-practice/src/day009/picture_puzzle/images/qiuzhu.png"));
        qiuzhuButton.setBounds(626, 444, 108, 45);
        this.add(qiuzhuButton);

        JButton chongzhiButton = new JButton(new ImageIcon("study-practice/src/day009/picture_puzzle/images/chongzhi.png"));
        chongzhiButton.setBounds(786, 444, 108, 45);
        this.add(chongzhiButton);

        JLabel beijingLabel = new JLabel(new ImageIcon("study-practice/src/day009/picture_puzzle/images/background.png"));
        beijingLabel.setBounds(0, 0, 960, 530);
        this.add(beijingLabel);
    }

    public void initFrame() {
        this.setSize(960, 565);
        this.setTitle("动漫拼图");
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
    }
}
